package com.pengtu.gsj.service.impl.check.lzgl.overloadMan;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.lzgl.GovernSpeedDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.lzgl.GovernSpeed;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.lzgl.overloadMan.OffSiteEnforService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;


@Service
@Transactional
public class OffSiteEnforServiceImpl implements OffSiteEnforService {

	@Autowired
	public SystemManageService systemManageService;
	@Autowired
	public CheckTableService checkTableService;
	@Autowired
	public GovernSpeedDao governSpeedDao;
	
	@Override
	public Map<String, Object> getOffSiteEnfors(String startTime, String endTime, CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Attachment> list = new ArrayList<Attachment>();
		List<Attachment> resultAtts = new ArrayList<Attachment>();
		List<String> resLists = Arrays.asList("0", "0");
		List<String> staNames = null;
		GovernSpeed governSpeed = new GovernSpeed();
		
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		String itemIds = (String)jsonObject.get("itemProjectId");
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		double reserveMark = Double.parseDouble((String)jsonObject.get("reserveMark"));
		double tableFull = MathsUtils.sum(yearFull, yearPart, poitMark, reserveMark);
		double total = 0;
		
		
		//根据时间、标签查询是否有附件
		list = systemManageService.getCheckAttachments(startTime, endTime, tag);
		map.put("att", list);
		
		//1、规章制度得分
		list = systemManageService.getCheckAttachments(startTime, endTime, "zcgl_fxcz");
		for (Attachment attachment : list) {
			if (0 < attachment.getFileName().indexOf("不停车") && "0".equals(resLists.get(0))) {
				resLists.set(0, "1");
			}
			if (0 < attachment.getFileName().indexOf("非现场") && "0".equals(resLists.get(1))) {
				resLists.set(1, "1");
			}
			if (0 < attachment.getFileName().indexOf("非现场") || 0 < attachment.getFileName().indexOf("治超")) {
				resultAtts.add(attachment);
			}
		}
		total += "1".equals(resLists.get(0)) ? yearFull/2 : 0;
		total += "1".equals(resLists.get(1)) ? yearFull/2 : 0;
		map.put("scoreOne", total);
		
		//2、试点得分
		if (0 < resultAtts.size()) {
			total += yearPart;
			map.put("scoreTwo", yearPart);
		}
		if (StringUtils.isNotEmpty(itemIds)) {
			//通过逗号将每个id切割出来，并搜索算分
			String[] idArrs = itemIds.split(",");
			//每次检索都初始化保存站点名称的List
			staNames = new ArrayList<String>();
			for (int i = 0; i < idArrs.length; i++) {
				//根据tag检索是否有附件
				if( i > 3) {
					break;
				}
				governSpeed = governSpeedDao.get(idArrs[i]);
				//得到所有的检测站的名称
				staNames.add(governSpeed.getStationName());
			}
			map.put("flag", true);
			//3、其他的都默认满分
			total += poitMark + reserveMark;
		}
		//根据总得分算出完善度
		double percentage = MathsUtils.divDouble(total*100, tableFull, 2);
		//只有当系统完善度和检查得分不相等的情况下才更新数据  避免每次搜索都更新数据
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("att", list);
		map.put("scoreThree", poitMark);
		map.put("scoreFour", reserveMark);
		map.put("staNames", staNames);
		map.put("resultAtts", resultAtts);
		map.put("percent", percentage);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		return map;
	}
}
